package com.ruoyi.flowable.controller;

import java.util.HashMap;

import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.mapper.CommonMapper;
import com.ruoyi.flowable.domain.dto.FlowTaskDto;
import com.ruoyi.flowable.domain.vo.WorkflowDealBodyVo;
import com.ruoyi.flowable.service.IFlowDefinitionService;
import com.ruoyi.flowable.service.IFlowTaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 工作流程定义
 * </p>
 *
 * @author Tony
 * @date 2021-04-03
 */
@Slf4j
@Api(tags = "流程定义")
@RestController
@RequestMapping("/workflow")
public class WorkflowController {

	@Autowired
	private IFlowDefinitionService flowDefinitionService;

	@Autowired
	private IFlowTaskService flowTaskService;

	@Autowired
	private CommonMapper commonMapper;
	
	@Autowired
    private RuntimeService runtimeService;
	
	@Autowired
    private HistoryService historyService;

	@PostMapping("commit")
	public AjaxResult commit(@RequestBody WorkflowDealBodyVo vo) {
		String defineId = getProcessDefineIdByBusiness(vo.getBizType());
		HashMap<String, Object> variables = commonMapper.selectDataById("BIZ_OA_LEAVE_INFO", vo.getBizId());
		return flowDefinitionService.startProcessInstanceById(defineId, vo, variables);
	}

	@PostMapping("getFlowRecordList")
	@ApiOperation(value = "流程历史流转记录", response = FlowTaskDto.class)
	public AjaxResult getFlowRecordList(@RequestBody WorkflowDealBodyVo vo) {
		ProcessInstance result = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(vo.getBusinessKey()).singleResult();
		if(result != null) {
			return flowTaskService.flowRecord(result.getId(), result.getDeploymentId());
		} else {
			HistoricProcessInstance result2 = historyService.createHistoricProcessInstanceQuery()
					.processInstanceBusinessKey(vo.getBusinessKey()).singleResult();
			if(result2 != null) {
				return flowTaskService.flowRecord(result2.getId(), result2.getDeploymentId());
			}
		}
		return AjaxResult.error("找不到的流程");
	}
	
	
	 /**
     * 流程节点信息
     *
     * @param vo 流程实例id
     * @return
     */
	@PostMapping("/flowXmlAndNode")
    public AjaxResult flowXmlAndNode(@RequestBody WorkflowDealBodyVo vo) {
    	ProcessInstance result = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(vo.getBusinessKey()).singleResult();
		if(result != null) {
			return flowTaskService.flowXmlAndNode(result.getId(), result.getProcessDefinitionId());
		} else if(StringUtil.isNotEmpty(vo.getBizType())){
			return flowTaskService.flowXmlAndNode(null, "flow_3f8whxmj:7:37504");
		}
		return AjaxResult.error("找不到的流程");
    }

	
	private String getProcessDefineIdByBusiness(String tableName) {
		return "flow_3f8whxmj:7:37504";
	}

}
